home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / user_interface.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.1 KB  |  200 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import os
  6. import pwd
  7. import sys
  8. import logging
  9. import subprocess
  10. import webbrowser
  11. import gettext
  12. from gettext import gettext as _
  13. from checkbox.contrib.REThread import REThread
  14. from checkbox.lib.environ import add_variable, get_variable, remove_variable
  15. from checkbox.lib.iterator import NEXT
  16.  
  17. class UserInterface(object):
  18.     '''Abstract base class for encapsulating the workflow and common code for
  19.        any user interface implementation (like GTK, Qt, or CLI).
  20.  
  21.        A concrete subclass must implement all the abstract show_* methods.'''
  22.     
  23.     def __init__(self, title, data_path = None):
  24.         self.title = title
  25.         self.data_path = data_path
  26.         self.direction = NEXT
  27.         self.gettext_domain = 'checkbox'
  28.         gettext.textdomain(self.gettext_domain)
  29.  
  30.     
  31.     def do_function(self, function, *args, **kwargs):
  32.         thread = REThread(target = function, name = 'do_function', args = args, kwargs = kwargs)
  33.         thread.start()
  34.         while thread.isAlive():
  35.             self.show_pulse()
  36.             thread.join(0.1)
  37.         thread.exc_raise()
  38.         return thread.return_value()
  39.  
  40.     
  41.     def show_error(self, title, text):
  42.         logging.error('%s: %s', title, text)
  43.  
  44.     
  45.     def show_wait(self, message, function, *args, **kwargs):
  46.         self.do_function(function, *args, **kwargs)
  47.  
  48.     
  49.     def show_pulse(self):
  50.         pass
  51.  
  52.     
  53.     def show_intro(self, title, text):
  54.         raise NotImplementedError, 'this function must be overridden by subclasses'
  55.  
  56.     
  57.     def show_category(self, title, text, category):
  58.         raise NotImplementedError, 'this function must be overridden by subclasses'
  59.  
  60.     
  61.     def show_test(self, test, result):
  62.         raise NotImplementedError, 'this function must be overridden by subclasses'
  63.  
  64.     
  65.     def show_exchange(self, authentication, message = None):
  66.         raise NotImplementedError, 'this function must be overridden by subclasses'
  67.  
  68.     
  69.     def show_final(self, message):
  70.         raise NotImplementedError, 'this function must be overridden by subclasses'
  71.  
  72.     
  73.     def show_url(self, url):
  74.         '''Open the given URL in a new browser window.
  75.  
  76.         Display an error dialog if everything fails.'''
  77.         (r, w) = os.pipe()
  78.         if os.fork() > 0:
  79.             os.close(w)
  80.             (pid, status) = os.wait()
  81.             if status:
  82.                 title = _('Unable to start web browser')
  83.                 error = _('Unable to start web browser to open %s.' % url)
  84.                 message = os.fdopen(r).readline()
  85.                 if message:
  86.                     error += '\n' + message
  87.                 
  88.                 self.show_error(title, error)
  89.             
  90.             
  91.             try:
  92.                 os.close(r)
  93.             except OSError:
  94.                 pass
  95.  
  96.             return None
  97.         os.setsid()
  98.         os.close(r)
  99.         
  100.         try:
  101.             uid = int(get_variable('SUDO_UID'))
  102.             gid = int(get_variable('SUDO_GID'))
  103.             sudo_prefix = [
  104.                 'sudo',
  105.                 '-H',
  106.                 '-u',
  107.                 '#%s' % uid]
  108.         except TypeError:
  109.             os.fork() > 0
  110.             os.fork() > 0
  111.             uid = os.getuid()
  112.             gid = None
  113.             sudo_prefix = []
  114.         except:
  115.             os.fork() > 0
  116.  
  117.         
  118.         try:
  119.             
  120.             try:
  121.                 if os.getenv('DISPLAY') and subprocess.call([
  122.                     'pgrep',
  123.                     '-x',
  124.                     '-u',
  125.                     str(uid),
  126.                     'ksmserver'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) == 0:
  127.                     subprocess.call(sudo_prefix + [
  128.                         'kfmclient',
  129.                         'openURL',
  130.                         url])
  131.                     sys.exit(0)
  132.             except OSError:
  133.                 os.fork() > 0
  134.                 os.fork() > 0
  135.             except:
  136.                 os.fork() > 0
  137.  
  138.             
  139.             try:
  140.                 if os.getenv('DISPLAY') and subprocess.call([
  141.                     'pgrep',
  142.                     '-x',
  143.                     '-u',
  144.                     str(uid),
  145.                     'gnome-panel'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) == 0:
  146.                     gct = subprocess.Popen(sudo_prefix + [
  147.                         'gconftool',
  148.                         '--get',
  149.                         '/desktop/gnome/url-handlers/http/command'], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  150.                     if gct.returncode == 0:
  151.                         preferred_browser = gct.communicate()[0]
  152.                         browser = re.match('((firefox|seamonkey|flock)[^\\s]*)', preferred_browser)
  153.                         if browser:
  154.                             subprocess.call(sudo_prefix + [
  155.                                 browser.group(0),
  156.                                 '-new-window',
  157.                                 url])
  158.                             sys.exit(0)
  159.                         
  160.                         browser = re.match('(epiphany[^\\s]*)', preferred_browser)
  161.                         if browser:
  162.                             subprocess.call(sudo_prefix + [
  163.                                 browser.group(0),
  164.                                 '--new-window',
  165.                                 url])
  166.                             sys.exit(0)
  167.                         
  168.                     
  169.                     if subprocess.call(sudo_prefix + [
  170.                         'gnome-open',
  171.                         url]) == 0:
  172.                         sys.exit(0)
  173.                     
  174.             except OSError:
  175.                 os.fork() > 0
  176.                 os.fork() > 0
  177.             except:
  178.                 os.fork() > 0
  179.  
  180.             if uid and gid:
  181.                 os.setgroups([
  182.                     gid])
  183.                 os.setgid(gid)
  184.                 os.setuid(uid)
  185.                 remove_variable('SUDO_USER')
  186.                 add_variable('HOME', pwd.getpwuid(uid).pw_dir)
  187.             
  188.             webbrowser.open(url, new = True, autoraise = True)
  189.             sys.exit(0)
  190.         except Exception:
  191.             os.fork() > 0
  192.             e = os.fork() > 0
  193.             os.write(w, str(e))
  194.             sys.exit(1)
  195.         except:
  196.             os.fork() > 0
  197.  
  198.  
  199.  
  200.